package threading;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
 
public class App {
    private static BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10);
     
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    producer();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    consumer();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        Thread.sleep(1000);
        t2.start();
        t1.join();
        t2.join();
    }
    private static  void producer() throws InterruptedException {
        Random random = new Random();
        while(true) {
            queue.put(random.nextInt(100));
        }
    }
    private static  void consumer() throws InterruptedException {
        Random random = new Random();
        while(true) {
            Thread.sleep(100);
             
            if(random.nextInt(5) == 0) {//difference between this line and random.next(100) and how it works, is it taking value out when 0 comes
            	// or how and where 0 comes at the end or in between?
//               System.out.print("before zero size was:   "+queue.size()+"    ");
            	Integer value = queue.take();// this take method is friendly? 
                //it will wait to take action unless queue is filled completely by put method
                 
                System.out.println("Taken value: " + value + "; Queue size is: " + queue.size());
                //why after taking out one element, queue size is 10 sometimes
            }
        }
    }
}